package com.muh.config;

import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;

import javax.net.ssl.SSLContext;

@Configuration
public class ElasticsearchConfig extends ElasticsearchConfiguration {
    @Override
    public ClientConfiguration clientConfiguration() {
        try {
            // 创建信任所有证书的SSL上下文
            SSLContext sslContext = new SSLContextBuilder()
                    .loadTrustMaterial(null, (chain, authType) -> true)
                    .build();

            return ClientConfiguration.builder()
                    .connectedTo("localhost:9200")
                    .usingSsl(sslContext, NoopHostnameVerifier.INSTANCE)
                    .withBasicAuth("elastic", "elastic") // 替换实际密码
                    .withConnectTimeout(10_000)
                    .withSocketTimeout(30_000)
                    .build();
        } catch (Exception e) {
            throw new RuntimeException("SSL配置失败", e);
        }
    }
}
